<?php
Yii::import('SolrUtil');
class HomeController extends Controller
{

    public function filters()
    {
        return array(
            //'accessControl', // perform access control for CRUD operations
            array('application.components.CLoginFilter'),
        );
    }

	/**
	 * Declares class-based actions.
	 */
	public function actions() {
		return array();
	}

    public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('index','view',),
                'users'=>array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('gethomeinfo','update'),
                'users'=>array('@'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
        // echo $_SESSION['username'];
        // echo Yii::app()->user->getName();
        // echo "111";
        // $options = array
        // (
        //     'hostname' => 'localhost',
        //     'port'     => '8080',
        //     'path'=>'solr/goods_en',
        // );

        // $client = new SolrClient($options);

        // //This will erase the entire index
        // $client->deleteByQuery("*:*");
        // $client->commit();
    }

    /**
     * 得到首页推荐(按更新时间排序，支持翻页)
     * @param int $updatetime
     */
    public function actionGetHomeInfo($updatetime=-1)
    {
        $resultArr = array();
        $local = CommonUtils::getLocal();

        if ($local==_LOCAL_ZN) {
            $desc_field ='desc_zn';
        } else {
            $desc_field ='desc_en';
        }
        $query['select'] = array('channel','image','link','updatetime','desc_zn');
        $query['limit'] = _LIMITED_PAGE_SIZE;
        $query['sort'] = array('updatetime'=>EMongoCriteria::SORT_DESC);
        if ($updatetime!=-1) {
            $query['conditions'] = array('updatetime'=>array('<'=>(int)$updatetime));
        }
        $result = HomeInfo::model()->findAll($query);

        $resultArr = array();
        foreach ($result as $key => &$value) {
            $item['desc'] = $value['desc_zn']; 
            $item['id'] = (string)$value['_id'];
            $item['channel'] = $value['channel'];
            $item['link'] = $value['link'];
            $item['image'] = $value['image'];
            $item['updatetime'] = $value['updatetime'];
            array_push($resultArr, $item);
        }
        echoToMobile($resultArr);
    }

    /**
     * 得到首页广告
     *
     */
    public function actionGetHomeAd()
    {
        $query = array(
            'conditions' => array(
                'channel'=>array('=='=>_CHANNEL_HOME),
            ),
            'select'=>array('images.image','images.link','images.linktype'),
        );
        $result = Ad::model()->find($query);
        if(!empty($result)) {
            echoToMobile(ModelConvertUtil::convert($result->images));
        } else {
            echoToMobile(array());
        }
    }

    /**
     * 全站搜索
     * @param $key
     * @param int $start
     * @param string $sort
     */
    public function actionSearch($key,$filter = null,$start=0)
    {
        $local = CommonUtils::getLocal();

        if ($local==_LOCAL_ZN) {
            $local = _SOLR_GOODS_ZN;
        } else {
            $local = _SOLR_GOODS_EN;
        }

        $solrUtil = new SolrUtil(_SOLR_ADDRESS, $local);
        $fields = 'id originPrice price name icon';
        $queryKey = "name:{$key} + technology:{$key} + brandname:{$key}";
        if(!empty($filter))
            $queryKey = "(name:{$key} OR technology:{$key} OR brandname:{$key}) AND (channel:{$filter})";
        $result = $solrUtil->select(array('q'=>$queryKey,'pageSize'=>10,'page'=>$start,'fl'=>$fields,'facet.field'=>array('channel')));
        $body = array();
        $body['nums'] = $result['data']['response']['numFound'];
        
        $body['fieldnum'] = array('GOODS'=>0,'HOTEL'=>0,'DINER'=>0,'TRAVEL'=>0,'FASHIONINFO'=>0,);
        foreach ($result['data']['facet_counts']['facet_fields']['channel'] as $key => $value) {
            if(!empty($value)&& $value == "GOODS")
            {
                $body['fieldnum']['GOODS'] = $result['data']['facet_counts']['facet_fields']['channel'] [$key+1];
            }
            if(!empty($value)&& $value == "HOTEL")
            {
                $body['fieldnum']['HOTEL'] = $result['data']['facet_counts']['facet_fields']['channel'] [$key+1];
            }
            if(!empty($value)&& $value == "DINER")
            {
                $body['fieldnum']['DINER'] = $result['data']['facet_counts']['facet_fields']['channel'] [$key+1];
            }
            if(!empty($value)&& $value == "TRAVEL")
            {
                $body['fieldnum']['TRAVEL'] = $result['data']['facet_counts']['facet_fields']['channel'] [$key+1];
            }
            if(!empty($value)&& $value == "FASHIONINFO")
            {
                $body['fieldnum']['FASHIONINFO'] = $result['data']['facet_counts']['facet_fields']['channel'] [$key+1];
            }
        }
        $body['docs'] = $result['data']['response']['docs'];

        echoToMobile($body);
    }
}